package com.hiwan.unions.activity.main;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.base.RootActivity;
import com.hiwan.unions.activity.loginRegister.RegisterPasswordUpdateActivity;
import com.hiwan.unions.adapter.CityAdapter;
import com.hiwan.unions.adapter.bean.MembersAdresssBean;
import com.hiwan.unions.application.ConstantsValues;
import com.hiwan.unions.application.exception.ApiException;
import com.hiwan.unions.util.CheckUtils;
import com.hiwan.unions.util.JsonUtil;
import com.hiwan.unions.util.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by llg on 2016/4/5.
 */
public class CityActivity extends RootActivity {
    private ListView myListview;
    private TextView ItemCityView;
    private List<String> list = new ArrayList<String>();
    private CityAdapter adapter;
    private ArrayList<MembersAdresssBean> arry_address = new ArrayList<MembersAdresssBean>();
    MembersAdresssBean bean;
    private String city;

    @Override
    protected int getLayout() {
        return R.layout.activity_city;
    }

    @Override
    protected void initData() {
        if (getIntent().getExtras() != null) {
            bundle = getIntent().getExtras();
        }
        stringArrayList(Utils.getJson(this, "city.txt"));
        adapter.notifyDataSetChanged();
    }

    private void stringArrayList(String s) {
        try {
            JSONObject jsonObject = new JSONObject(s);
            JSONArray jsonArray = JsonUtil.getJSONArray(jsonObject, "RECORDS");
            for (int i = 0; i < jsonArray.length(); i++) {
                jsonObject = JsonUtil.getJSONObject(jsonArray, i);
                if (JsonUtil.getString(jsonObject, "level").equals("2")) {
                    bean = new MembersAdresssBean();
                    bean.setArea_code(JsonUtil.getString(jsonObject, "area_code"));
                    try {
                        city = CheckUtils.checkCity(JsonUtil.getString(jsonObject, "area_name"));
                    } catch (ApiException e) {
                        city = e.getMessage();
                    }
                    bean.setArea_name(city);
                    list.add(city);
                    bean.setLevel(JsonUtil.getString(jsonObject, "level"));
                    bean.setParent_area_code(JsonUtil.getString(jsonObject, "parent_area_code"));
                    arry_address.add(bean);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private String stringCode(String s) {
        for (MembersAdresssBean bean : arry_address) {
            if (CheckUtils.containsString(bean.getArea_name(), s)) {
                return bean.getArea_code();
            }
        }
        if (CheckUtils.containsString(s, "杭州")) {
            return "330100";
        }
        return "000000";
    }

    public void ButtonOnClick(View view) {
        fin(userInfoC.getString(ConstantsValues.CITYNAMEOne, ""));
    }


    @Override
    protected void onViewCreated() {
        super.onViewCreated();
        ItemCityView = findView(R.id.ItemCityView);
        ItemCityView.setText(userInfoC.getString(ConstantsValues.CITYNAMEOne, "成都市"));
        myListview = findView(R.id.myListview);
        adapter = new CityAdapter(this, list, userInfoC);
        myListview.setAdapter(adapter);
        myListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                IntentConfirm(position, list);
            }
        });
        setTitleTxt("城市选择");

    }


    protected void IntentConfirm(int position, List<String> list) {
        fin(list.get(position));
    }

    private void fin(String city) {

        SharedPreferences.Editor editor = userInfoC.edit();
        editor.putString(ConstantsValues.CITYNAME, city);
        editor.putString(ConstantsValues.CITYNAMECODE, stringCode(city));
        editor.commit();

        Intent intent = new Intent();
        bundle = new Bundle();
        bundle.putString("city", city);
        intent.putExtras(bundle);
        //设置返回数据
        setResult(RESULT_CANCELED, intent);
        //关闭Activity
        finish();
    }

    // --离线数据
    private void local() {

    }

}
